If statement¶
Introduction of if statement¶
An if statement usually creates a 2-to-1 multiplexer, selecting one input if the condition is true, and the other input if the condition is false.

| Verilog | |
|---|---|
This is equivalent to using a continuous assignment with a conditional operator:
| Verilog | |
|---|---|
However, the procedural if statement provides a new way to make mistakes. The circuit is combinational only if out is always assigned a value.
Practice¶
Problem statement¶
Build a 2-to-1 mux that chooses between
aandb. Choosebif bothsel_b1andsel_b2are true. Otherwise, choosea. Do the same twice, once usingassignstatements and once using a procedural if statement.
| sel_b1 | sel_b2 | out_assign out_always |
|---|---|---|
| 0 | 0 | a |
| 0 | 1 | a |
| 1 | 0 | a |
| 1 | 1 | b |
| Verilog | |
|---|---|